package multiIntro;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import polyglot.ast.Call;
import polyglot.ast.Expr;
import polyglot.ast.Node;
import polyglot.ast.NodeFactory;
import polyglot.ast.Receiver;
import polyglot.ext.jl.ast.Call_c;
import polyglot.ext.jl.parse.Name;
import polyglot.types.TypeSystem;
import polyglot.util.Position;
import polyglot.util.TypedList;
import xaj.xc.ast.NodeXAJ;

//importsymbol multiIntro.Eval;

public class MethodInvocationEval extends Call_c implements Call, NodeXAJ, xaj.lang.Map {
	/*
	@grammar extends method_invocation using Eval {
		MethodInvocationEval -> n = name DOT e = Eval LPAREN a = argument_list_opt RPAREN;
	}*/
	
	private Map map;
	
	public MethodInvocationEval(Position pos) {
		this(pos, null, null, new TypedList(new LinkedList(), Expr.class, false));
	}
	
	public MethodInvocationEval(Position pos, Receiver target, String name,
			List arguments) {
		super(pos, target, name, arguments);
		this.map = new HashMap();
		System.out.println("Criou um objeto MethodInvocationEval");
	}
	
	public boolean add(String name, Object value) {
		if(!map.containsKey(name)) {
			map.put(name, value);
			return true;
		} else
			return false;
	}

	public Iterator iterator() {
		return map.keySet().iterator();
	}

	public Object get(String name) {
		return map.get(name);
	}
	
	public void set(String name, Object value) {
		if(map.containsKey(name)) {
			map.keySet();
			map.put(name, value);
		}
	}

	public Node desugar(NodeFactory ft, TypeSystem ts) {
		System.out.println("Desugar do MethodInvocationEval");
		Name name = (Name) get("n");
		List args = (List) get("a");
		Eval eval = (Eval) get("e");
		System.out.println(name.prefix);
		return ft.Call(position(), name.prefix == null ? name.toReceiver() : name.prefix.toReceiver(),
				((Eval) eval.desugar(ft, ts)).getEvaluetedValue(), args);
	}	
}